package com.itheima.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtTest {
    public static void main(String[] args) {
        //
        long l = System.currentTimeMillis()+1000*60*60*24*7;
        Date date = new Date(l);
        //生成jwt令牌
        JwtBuilder builder = Jwts.builder()
                .setId("666")//设置jwt编码
                .setSubject("畅购商城")//设置jwt主题
                .setIssuedAt(new Date())//设置jwt签发日期
                .setExpiration(date)//设置jwt的过期时间
                .claim("roles","admin")
                .claim("company","itheiamama")
                .signWith(SignatureAlgorithm.HS256,"itheima");//
        //生成jwt
        String compact = builder.compact();
        System.out.println(compact);
        //解析jwt
        Claims itheima = Jwts.parser().setSigningKey("itheima").parseClaimsJws(compact).getBody();
        System.out.println(itheima);
    }
}
